package com.anbang.app.ui.guide

import android.animation.ArgbEvaluator
import android.animation.ObjectAnimator
import android.os.Handler
import android.view.View
import android.widget.HorizontalScrollView
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.viewpager.widget.ViewPager
import com.anbang.app.R
import com.anbang.framework.utils.log


/**
 * @Description:
 * @Author: AnBang
 * @Date: 2019/1/12 16:05
 */
//todo  这种继承两个 接口的 方式要格外注意  一定要new 两次分别设置!!!!!!
class GuideViewPageTransformer : ViewPager.PageTransformer, ViewPager.OnPageChangeListener {

    private var nowPosition = 0

    private var pageChange = true

    private val ROT_MOD = -15f


    /**
     * 此方法是滑动的时候每一个页面View都会调用该方法
     * @param page:当前的页面
     * @param transformPos:当前滑动的位置-3  -2 -1.0 左滑完成  0.0中间  1.0 右滑完成 2 3
     * 视差效果：在View正常滑动的情况下，给当前View或者当前view里面的每一个子view来一个加速度
     * 而且每一个加速度大小不一样。
     *
     */
    override fun transformPage(page: View, transformPos: Float) {

        val rootView = page.findViewById<View>(R.id.rootView)
        val bg1Iv = page.findViewById<ImageView>(R.id.bg1Iv)
        val bg2Iv = page.findViewById<ImageView>(R.id.bg2Iv)
        val scrollView = page.findViewById<HorizontalScrollView>(R.id.scrollView)
        val containerFl = page.findViewById<View>(R.id.containerFl)

        val colorGreen = ContextCompat.getColor(page.context, R.color.bg1_green)
        val colorBlue = ContextCompat.getColor(page.context, R.color.bg2_blue)

        val viewPosition = page.tag
        log("=========>viewPosition->$viewPosition========transformPos->$transformPos")
        //颜色选择器
        val argbEvaluator = ArgbEvaluator()

        //布局为当前页面
        if (viewPosition == nowPosition) {
            var color = colorGreen

            when (nowPosition) {
                0 -> {
                    color = argbEvaluator.evaluate(Math.abs(transformPos), colorGreen, colorBlue) as Int
                }

                1 -> {
                    color = argbEvaluator.evaluate(Math.abs(transformPos), colorBlue, colorGreen) as Int
                }

                2 -> {
                    color = argbEvaluator.evaluate(Math.abs(transformPos), colorGreen, colorBlue) as Int
                }
            }

            //设置 ViewPage 整体背景颜色
            (rootView.parent as View).setBackgroundColor(color)
        }

        //页面改变 并且滚动完成后 开始计时更换页面
        if (transformPos == 0f && pageChange) {
            //置为false
            pageChange = false

            bg1Iv.visibility = View.VISIBLE
            bg2Iv.visibility = View.INVISIBLE

            Handler().postDelayed({
                bg1Iv.visibility = View.VISIBLE
                bg2Iv.visibility = View.VISIBLE

                ObjectAnimator.ofFloat(bg1Iv, "translationX", 0f, -bg1Iv.width.toFloat()).apply {
                    duration = 400
                    addUpdateListener {
                        scrollView.smoothScrollTo((scrollView.width * animatedFraction).toInt(), 0)
                    }
                }.start()

                ObjectAnimator.ofFloat(bg2Iv, "translationX", bg2Iv.width.toFloat(), 0f).apply {
                    duration = 400
                }.start()
            }, 600)
        }

        if (transformPos == -1f || transformPos == 1f) {
            //还原
            bg1Iv.translationX = 0f
            bg2Iv.translationX = 0f
            bg2Iv.visibility = View.INVISIBLE
            scrollView.smoothScrollTo(0, 0)
        } else {
            //注意 这里 必须在-1 到 1的范围内
            if (transformPos > -1f && transformPos < 1f) {
                val width = bg1Iv.width
                val height = bg1Iv.height
                val rotation = ROT_MOD * transformPos * -1.25f
                containerFl.pivotX = width * 0.5f
                containerFl.pivotY = height.toFloat()
                containerFl.rotation = rotation
            }
        }
    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
    }

    override fun onPageSelected(position: Int) {
        nowPosition = position
        pageChange = true
    }
}